package com.superlz.demo.security.web.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * 过滤器
 *
 *  示例：响应耗时，使用postMan请求（spring管理注入）
 *
 *  方法1：实现Filter接口，重写对应方法，类上加上@Component注解
 *  方法2：使用@Configuration和@Bean，装配Spring过滤器(不能使用@Component的情况)->（例：WebConfig）
 *
 * @Author: lz
 * @Description:
 * @Date: 2019/7/3
 */
/*@Component*/
public class TimeFilter implements Filter {
    /**
     * 初始化
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("time filter init");

    }

    /**
     * 处理过滤逻辑
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("time filter start");
        long start = System.currentTimeMillis();
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("time filter 耗时:"+ (System.currentTimeMillis() - start));
        System.out.println("time filter finish");

    }

    /**
     * 销毁
     */
    @Override
    public void destroy() {
        System.out.println("time filter destroy");
    }
}
